/*
 * Denso D151811-0360 8bit ADC with serial interface driver
 *
 * we use two timers here: one for three wire, 62.5KHz serial interface implementation
 * and the other for the 1MHz clock output (second one is optional)
 *
 *  Created on: Nov 22, 2012
 *      Author: Andrey Belomutskiy
 */

#include "denso_adc_io.h"
#include "hal.h"
#include "main.h"

#ifdef DENSO_1MHZ_CLOCK
PWMDriver *denso_primary_clock = &DENSO_PRIMARY_CLOCK_TIMER;

/**
 * we need twice the frequency, 2 ticks for wave cycle, no callback
 */
static PWMConfig denso_primary_clock_cfg = { 2 * DENSO_PRIMARY_CLOCK_FREQUENCY, 2,
		NULL, { // now go 4 channels
		{ PWM_OUTPUT_DISABLED, NULL }, // CH1
				{ PWM_OUTPUT_DISABLED, NULL }, // CH2
				{ PWM_OUTPUT_ACTIVE_LOW, NULL }, // CH3
				{ PWM_OUTPUT_DISABLED, NULL }, // CH4
		},
		/* HW dependent part.*/
		0 };

#endif

void initDensoAdc() {
#ifdef DENSO_1MHZ_CLOCK

	pwmStart(denso_primary_clock, &denso_primary_clock_cfg);

	// setting 50% duty cycle wave
	pwmEnableChannel(denso_primary_clock, DENSO_PRIMARY_CLOCK_CHANNEL,
			PWM_FRACTION_TO_WIDTH(denso_primary_clock, 2, 1) );

	palSetPadMode(DENSO_PRIMARY_CLOCK_IO_PORT, DENSO_PRIMARY_CLOCK_PIN,
			PAL_MODE_ALTERNATE(DENSO_PRIMARY_CLOCK_MODE_ALT));
#endif
}
